Skip to content

Fix match statement issue#11363

Merged
gramster merged 4 commits intomicrosoft:mainfrom
gramster:fix-match-statement-issue
Apr 14, 2026
Merged

Fix match statement issue#11363
gramster merged 4 commits intomicrosoft:mainfrom
gramster:fix-match-statement-issue

Conversation

@gramster
Copy link
Copy Markdown
Member

@gramster gramster commented Apr 7, 2026

Fix pattern matching regression for variadic tuple types
When a tuple with an unbounded entry (e.g., tuple[int, *tuple[str, ...], int])
is matched against a pattern shorter than the tuple, the unbounded entry is
removed to make lengths match. However, this loses the information that the
tuple was originally variadic and could have different lengths.

The fix tracks when an unbounded entry is removed (removedIndeterminate flag)
and marks such matches as isPotentialNoMatch=true. This prevents the entire
variadic tuple from being eliminated in negative narrowing, preserving
reachability of subsequent match cases.

Fixes #10845

Copilot AI and others added 2 commits April 6, 2026 17:30
When a tuple with an unbounded entry (e.g., tuple[int, *tuple[str, ...], int])
is matched against a pattern shorter than the tuple, the unbounded entry is
removed to make lengths match. However, this loses the information that the
tuple was originally variadic and could have different lengths.

The fix tracks when an unbounded entry is removed (removedIndeterminate flag)
and marks such matches as isPotentialNoMatch=true. This prevents the entire
variadic tuple from being eliminated in negative narrowing, preserving
reachability of subsequent match cases.
@gramster gramster requested review from heejaechang and rchiodo April 7, 2026 00:36
@github-actions

This comment has been minimized.

Comment thread .gitignore

# Potential venv
.venv No newline at end of file
.venv
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot generated:
-134

Remove this entire hunk from the PR. These additions (serena/, .beads/, AGENTS.md) are unrelated to the pattern matching fix and appear to be local dev environment artifacts. Notably, AGENTS.md exists at the repo root and is already tracked — adding it to .gitignore signals wrong intent and could confuse contributors. Keep the PR focused on the bug fix.


// If the tuple contains an indeterminate entry, expand or remove that
// entry to match the length of the pattern if possible.
let expandedIndeterminate = false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot generated:
-1430

The comment documents downstream entanglement mechanics ("tupleIndeterminateIndex becomes -1, making isUnboundedTuple false... isPotentialNoMatch=true prevents incorrect elimination..."). While accurate today, this creates a maintenance trap — if anyone reorders the downstream isDefiniteMatch / isUnboundedTuple checks, the safety argument becomes silently stale. Consider simplifying to state intent: "Track that the original tuple was variadic; this information is lost when the unbounded entry is spliced out, and is needed to prevent incorrect definite-match classification."

@@ -1435,6 +1441,7 @@

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot generated:
-1442

After tupleIndeterminateIndex = -1, the resulting SequencePatternInfo will have isUnboundedTuple: false even though the original tuple IS unbounded. This works today because isPotentialNoMatch = true prevents elimination before isUnboundedTuple is consulted, but any future consumer of isUnboundedTuple that trusts it as ground truth will get incorrect information. Consider: isUnboundedTuple: removedIndeterminate || tupleIndeterminateIndex >= 0 at the push site (around line 1510) to preserve semantic accuracy.


// If the tuple contains an indeterminate entry, expand or remove that
// entry to match the length of the pattern if possible.
let expandedIndeterminate = false;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot generated:
-1430

Rewrite this comment to state intent, not downstream mechanics (unresolved from prior review). The current text documents a fragile ordering dependency ("isPotentialNoMatch=true prevents incorrect elimination at the isDefiniteMatch check before the isUnboundedTuple guard matters"). If anyone reorders those downstream checks, this safety argument becomes silently stale. Suggested replacement:

// Track that the original tuple was variadic; this information is lost when
// the unbounded entry is spliced out, and is needed to prevent incorrect
// definite-match classification downstream.
let removedIndeterminate = false;

Copy link
Copy Markdown
Collaborator

@rchiodo rchiodo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@gramster gramster merged commit 4da46a6 into microsoft:main Apr 14, 2026
11 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Methods of a variadic generic class become uncallable when specified with TypeVarTuple and a suffix

3 participants